#include "ad.h"
#include "ui_ad.h"
#include<QSqlQuery>
#include<QDebug>
#include<QKeyEvent>


ad::ad(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ad)
{
    ui->setupUi(this);

}

ad::~ad()
{
    delete ui;
}

//键盘enter按下即相应确定键
void ad::keyPressEvent(QKeyEvent *event){
    if(event->key()==Qt::Key_Enter){
        on_enter_clicked();
    }
}

//返回初始菜单界面
void ad::on_return_2_clicked()
{
    emit display(0);
}

//管理员登录确认键
void ad::on_enter_clicked()
{
    ui->tip->setText("");
    QString id=ui->line1->text();
    QString pwd=ui->line2->text();
    QSqlQuery query;
    query.exec("select id,password from ad");
    bool exist=false;

    //查找账号是否存在
    while(query.next()){
        QString ID=query.value(0).toString();
        //列出可用账户
        qDebug()<<"账户"<<ID<<'\n';
        if(id==ID){
            exist=true;
            break;
        }
    }
    if(!exist)ui->tip->setText("账号不存在");
    else{
        if(pwd!=query.value(1).toString())
            ui->tip->setText("密码错误，请重新输入！");

        else{
            //登录成功则转到管理员主界面
            emit display(4);

        }
    }

}

